<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :span="8"
                        :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.queryParams.datasourceType"></zh-form-select>
        <zh-form-select prop="fieldTypeName" label="字段类型名称" allowClear :span="8" :options="pageData.dictData.FILEDTYPE"
                        v-model="pageData.queryParams.fieldTypeName"></zh-form-select>
        <zh-form-input prop="templateStr" label="模板字符串" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.templateStr"></zh-form-input>
        <zh-form-query @queryClick="queryFieldMetaTemplate" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="字段模板列表" ref="fieldMetaTemplateTable"
              size="mini"
              :funcCode="'fieldMetaTemplate0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delFieldMetaTemplate(scope.row)"></el-button>
        </el-tooltip>
        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加字段模板" v-model="pageData.addFieldMetaTemplateDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :span="8"
                        :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.formData.datasourceType"></zh-form-select>
        <zh-form-select prop="fieldTypeName" label="字段类型名称" allowClear :span="8" :options="pageData.dictData.FILEDTYPE"
                        v-model="pageData.formData.fieldTypeName"></zh-form-select>
        <zh-form-input prop="templateStr" label="模板字符串" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.templateStr"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">

          <el-button @click="pageData.addFieldMetaTemplateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addFieldMetaTemplate">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改字段模板" v-model="pageData.editFieldMetaTemplateDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.editFormData.datasourceType"></zh-form-select>
        <zh-form-select prop="fieldTypeName" label="字段类型名称" allowClear :options="pageData.dictData.FILEDTYPE"
                        v-model="pageData.editFormData.fieldTypeName"></zh-form-select>
        <zh-form-input prop="templateStr" label="模板字符串"
                       v-model="pageData.editFormData.templateStr"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editFieldMetaTemplateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editFieldMetaTemplate">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="fieldMetaTemplate">
import {Delete, Edit, Document} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const fieldMetaTemplateTable = ref();
const queryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  fieldTypeMetas: [],
  addFieldMetaTemplateDialogVisible: false,
  editFieldMetaTemplateDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'fieldTemplateId', title: '字段模板主键', visible: false, align: 'center', width: 100},
    {field: 'datasourceType', title: '数据源类型', visible: false, align: 'center', width: 100},
    {field: 'datasourceTypeName', title: '数据源类型', visible: true, align: 'center', width: 100},
    {field: 'fieldTypeName', title: '字段类型名称', visible: true, align: 'center', width: 100},
    {field: 'templateStr', title: '模板字符串', visible: true, align: 'center', width: 250},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 120, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    datasourceType: '', fieldTypeName: '', templateStr: ''
  },
  formData: {
    fieldTemplateId: '', datasourceType: '', fieldTypeName: '', templateStr: ''
  },
  editFormData: {
    fieldTemplateId: '', datasourceType: '', fieldTypeName: '', templateStr: ''
  },
  formRules: {},
  dictFields: 'DATASOURCETYPE,FILEDTYPE',
  dictData: [],
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryFieldMetaTemplate();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
  $api.common.send.post('fieldTypeMeta0001', {pageSize: 100}).then((res) => {
    if (res.code == 0) {
      pageData.fieldTypeMetas = res.result.list;
      let fieldTypes = Array.from(new Set(pageData.fieldTypeMetas.map(fieldTypeMeta => fieldTypeMeta.fieldTypeName)));
      fieldTypes = fieldTypes.map(fieldType => {return {dictKey: fieldType, dictValue: fieldType, option: 'children'}});
      pageData.dictData.FILEDTYPE = fieldTypes;
    }
  })
};
const queryFieldMetaTemplate = () => {
  fieldMetaTemplateTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addFieldMetaTemplateDialogVisible = true;
  pageData.formData = {
    fieldTemplateId: '', datasourceType: '', fieldTypeName: '', templateStr: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editFieldMetaTemplateDialogVisible = true;
  pageData.editFormData = {...scope.row}
}
const addFieldMetaTemplate = () => {
  $api.common.send.post('fieldMetaTemplate0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加字段模板成功');
      pageData.addFieldMetaTemplateDialogVisible = false;
      queryFieldMetaTemplate();
    }
  })
}
const editFieldMetaTemplate = () => {
  $api.common.send.post('fieldMetaTemplate0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改字段模板成功');
      pageData.editFieldMetaTemplateDialogVisible = false;
      queryFieldMetaTemplate();
    }
  })
}
const delFieldMetaTemplate = ({fieldTemplateId}) => {
  ElMessageBox.confirm('确定删除该字段模板?').then(() => {
    $api.common.send.post('fieldMetaTemplate0006', {fieldTemplateId: fieldTemplateId}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除字段模板成功');
        queryFieldMetaTemplate();
      }
    })
  })
}

</script>
<style lang="scss" scoped></style>
